package 剑指Offer._2130;

import general_class.TreeNode;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/8/18 16:45
 * 二叉树的镜像
 * 算法：树  深度优先搜索
 * 新建一个根节点，遍历原来的树，然后反向赋值
 */
public class _27 {

    public TreeNode mirrorTree(TreeNode root) {
        if (root == null) return null;
        TreeNode ans = new TreeNode(root.val);
        dfs(root, ans);
        return ans;
    }
    public void dfs(TreeNode root, TreeNode ans) {
        if (root == null) return;
        ans.val = root.val;
        if (root.left != null) {
            ans.right = new TreeNode(-1);
            dfs(root.left, ans.right);
        }
        if (root.right != null) {
            ans.left = new TreeNode(-1);
            dfs(root.right, ans.left);
        }
    }

    public TreeNode mirrorTree_perfect(TreeNode root) {
        if (root == null) return null;
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        mirrorTree_perfect(root.left);
        mirrorTree_perfect(root.right);
        return root;
    }

}
